package com.safe.ly_safe.activity;
import android.os.Build;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.safe.ly_safe.R;
import com.safe.ly_safe.base.BaseActivity;
import com.safe.ly_safe.base.BaseApplication;
import com.safe.ly_safe.control.presenter.LoginPresenter;
import com.safe.ly_safe.control.view.MainView;
import com.safe.ly_safe.utils.StatusBarUtil;
import com.safe.ly_safe.utils.SwitchButton;

import butterknife.BindView;

public class MainActivity extends BaseActivity implements MainView {

    @BindView(R.id.mTv)
    TextView mTv;

    private LoginPresenter loginPresenter;

    private long firstTime = 0;

    @Override
    protected void setFullScreen() {

    }

    @Override
    protected int setContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected String loadingMsg() {
        return "正在加载...";
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView() {
        loginPresenter = new LoginPresenter();
        loginPresenter.attachView(this);
        mTv = findViewById(R.id.mTv);
        mTv.setText("联易软件.");
        StatusBarUtil.setStatusBarLightMode(getWindow()); //设置状态栏黑色字体图标
        Log.i("MainActivity","测试代码提交>>>>");
    }

    @Override
    public void showLoading(int type) {
        mDialog.show();
    }

    @Override
    public void hideLoading(int type) {
        mDialog.dismiss();
    }

    @Override
    public void onSuccess(int type, Object result) {

    }

    @Override
    public void onError(int type, Throwable e) {

    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                long secondTime = System.currentTimeMillis();
                if (secondTime - firstTime > 2000) {                 //如果两次按键时间间隔大于2秒，则不退出
                    Toast.makeText(this,"再按一次退出程序",Toast.LENGTH_SHORT).show();
                    firstTime = secondTime;//更新firstTime
                    return true;
                } else {                                      //两次按键小于2秒时，退出应用
                    BaseApplication.getInstance().exitAll();  //finis掉所有activity
                    //System.exit(0);
                }
                break;
        }
        return super.onKeyUp(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(loginPresenter != null){
            loginPresenter.detachView();
        }
    }
}